<?php
include_once "lib/config.php";
include_once "header.php";


if(isset($_POST['save'])&& $_POST['save']!="")
{
	$data= $_POST['Sponcer'];
    $data['last_modified_date']=time();
    $data['last_modified_by']=$_SESSION['u_id'];
    
    $filename="";
	
    $msg_error="";
    
	if($_FILES['sponcer_photo']['name']!="")
    {
        include_once "thumb.php";
        if($_FILES['sponcer_photo']['type']=="image/jpeg" || $_FILES['sponcer_photo']['type']=="image/gif" || $_FILES['sponcer_photo']['type']=="image/png" || $_FILES['sponcer_photo']['type']=="image/jpg" || $_FILES['sponcer_photo']['type']=="image/JPEG" || $_FILES['sponcer_photo']['type']=="image/GIF" || $_FILES['sponcer_photo']['type']=="image/PNG" || $_FILES['sponcer_photo']['type']=="image/JPG")
        {
            list($width, $height) = getimagesize($_FILES['sponcer_photo']["tmp_name"]);
            $thumbfolder_val="../uploads/sponcers/thumbs/";
            $folder_val="../uploads/sponcers";
            if(!file_exists($folder_val))
            {
				mkdir($folder_val,0777);
			}
            $folder_val=$folder_val."/";
            
            $newfilename="";
            
            $newfilename=$_FILES['sponcer_photo']['name'];
            $path=$folder_val.$newfilename;
            
            if(file_exists($path))
            {
				$filenamearr=explode(".",$newfilename);
                
                $newfilename="";
                
                for($i=0;$i<count($filenamearr)-1;$i++)
                {
                    $newfilename=$newfilename.$filenamearr[$i].".";
                }
                $newfilename=substr($newfilename,0,strlen($newfilename)-1);
                $newfilename=$newfilename."_".time().".".$filenamearr[count($filenamearr)-1];
                $path=$folder_val.$newfilename;
			}
            
            if($width<100 || height<100)
            {
                if($_FILES['sponcer_photo']['size']<=1048576)
                {
                    $move= move_uploaded_file($_FILES['sponcer_photo']['tmp_name'], $path);
                    if($move)
                    {
                        $thumbfolder_val=$thumbfolder_val.$newfilename;
                        createThumbsforProfiles_widthequal_Transparent($path,$thumbfolder_val,85,87);
                        unlink($path);
                        copy($thumbfolder_val,$path);
                        $filename=$newfilename;
                    }
                    
                }
                else
                {
                    $msg_error="Image size should be less than or equal to 1 MB.";
                }
            }
        	else
            {
                $msg_error="Image width & height should be greater than 156px & 111px repectively.";
            }
        	
        }
		else
		{
			$msg_error="Prfile image extension should be one of the (jpg,jpeg,png,gif)";
		}    
    }
    else
    {
		$msg_error="Image is not found!";
	}
    
    if(!(strpos($data['link'],"http://")>=0))
    {
        $data['link']="http://".$data['link'];
    }
    
    if(isset($_POST['sponcer_id']) && $_POST['sponcer_id']>0)
	{
	    if($_FILES['sponcer_photo']['name']!="" && trim($filename)=="")
        {
            $msg_error="There is no record updated due to : ".$msg_error;
        }
        else
        {
            if($_FILES['sponcer_photo']['name']!="")
            {
                if(file_exists("../uploads/sponcers/".$data["image_url"]))
                {
                    unlink("../uploads/sponcers/".$data["image_url"]);
                }
                if(file_exists("../uploads/sponcers/thumbs/".$data["image_url"]))
                {
                    unlink("../uploads/sponcers/thumbs/".$data["image_url"]);
                }
                $data["image_url"]=$filename;
            }
            
            
            $check_query=DB::updatecheckExistance("af_sponcers",array("sponcer_name"=>$data['sponcer_name'],"link"=>$data['link']),array("id"=>$_POST['sponcer_id']));
        
    		if($check_query==0)
    		{
    		    $sponcerdetail=DB::fetch_array(DB::query("select sponcer_name,link from af_sponcers where id='".$_POST['sponcer_id']."'"));
                $insertdataarr=array("staff_id"=>$_SESSION['u_id'],"action"=>"Updated the sponcer ".$sponcerdetail[0]['sponcer_name'],"logtime"=>time());
                DB::insertData("stafflog",$insertdataarr);
                
    		    DB::updateData("af_sponcers",$data,array("id"=>$_POST['sponcer_id']));
    				
    			if(DB::$affectedrows>0)
    			{
    			    
    				$_SESSION['msg_success']="Sponcer updated successfully.";
                    redirect('sponcers.php');
    			}
    			else
    			{
    				$msg_error="There is no record updated.";
    			}
    		}
    		else
    		{
    			$msg_error="There is already one record exists for given sponcer name in selected type.";
    		}
        }
	    
	}
	else
	{
	    if($_FILES['sponcer_photo']['name']!="" && trim($filename)=="")
        {
            $msg_error="There is no record added due to : ".$msg_error;
        }
        else
        {
            $data["image_url"]=$filename;
    		$check_query=DB::checkExistance("af_sponcers",array("sponcer_name"=>$data['sponcer_name'],"link"=>$data['link']));
            
    		if($check_query==0)
    		{
    		    $data['added_date']=time();
                $data['added_by']=$_SESSION['u_id'];
                $data['last_modified_date']=time();
                $data['last_modified_by']=$_SESSION['u_id'];
                
    			$data['sponcer_status']="Active";
    			
                $insertdataarr=array("staff_id"=>$_SESSION['u_id'],"action"=>"Inserted the ".$data['sponcer_name']." sponcer ","logtime"=>time());
                DB::insertData("stafflog",$insertdataarr);
                
                DB::insertData("af_sponcers",$data);
    			
                if(DB::$affectedrows>0)
    			{
    			 
    				$_SESSION['msg_success']="Sponcer added successfully.";
    				redirect('sponcers.php');
    			}
    			else
    			{
    				$msg_error="There is no record added.";
    			}
    			
    		}
    		else
    		{
    			$msg_error="There is already one record exists for given sponcer name in selected sponcer type.";
    		}
        }
	}
	
}
$dob=date("d/m/Y");
if(isset($_POST['sponcer_id']) && $_POST['sponcer_id']>0)
{
	$conditionarr=array(":id"=>$_POST['sponcer_id']);
    $sponcerinfo=DB::fetch_array(DB::query("select * from af_sponcers where id=:id",$conditionarr));
    
    if(isset($sponcerinfo[0]))
    {
        $data=$sponcerinfo[0];
    }
    else
    {
        $data=array();
    }
    
   
}

?>
  		<div class="container_12" id="main_div">
           <form action="" method="post" onsubmit="return validateaddeditsponcer();" enctype="multipart/form-data">
			<br>
			<div class="grid_12" >
		    	<?php
            	if(isset($msg_success) && $msg_success!="")
            	{
            	?>
                <div>
                    <span class="notification n-success"><?php echo $msg_success; ?></span>
                </div>
                <?php
                }
                if(isset($msg_error) && $msg_error!="")
                {
                ?>
                <span class="notification n-error"><?php echo $msg_error; ?></span>
                <?php
				}
                ?>
		        <div class="module" >
		        		
		             <h2><span onclick="toggle_div('basic_body','toogleimg');" ><img src="images/desc.gif" height="5px" id="toogleimg">&nbsp;&nbsp;Sponcer Details</span></h2>
		                
		             <div class="module-body" id="basic_body">
		                
		                  	<p>
		                        <label>Sponcer Name<span style="color: red;">*</span></label>
		                        <input type="text" class="input-short" style="width: 280px;" id="name" name="Sponcer[sponcer_name]" value="<?php if(isset($data['sponcer_name']) && $data['sponcer_name']!=Null)echo $data['sponcer_name']; ?>"/>
		                        
		                    </p>
		                    <p>
		                        <label>Sponcer Link<span style="color: red;">*</span></label>
		                        http://<input type="text" class="input-short" style="width: 280px;" id="link" name="Sponcer[link]" value="<?php if(isset($data['link']) && $data['link']!=Null)echo $data['link']; ?>"/>
		                    </p>
                            
                            <p>
		                        <label>Sponcer Image<span style="color: red;">*</span></label>
		                        <input type="file" name="sponcer_photo" id="sponcer_photo" /><span style="color: lightgray;">(Note: Image size should be less than equals to 1 MB) </span><br /><br />
                                <input type="hidden" name="Sponcer[image_url]" id="image_url" value="<?php if(isset($data['image_url']) && $data['image_url']!=Null && $data['image_url']!="")echo $data['image_url']; ?>" />
                                <?php if(isset($data['image_url']) && $data['image_url']!=Null && $data['image_url']!="")
                                {
                                    ?>
                                    <img src="../uploads/sponcers/thumbs/<?php echo $data['image_url']; ?>"/>  
                                    <?php
                                }
                                ?>
		                    </p>
                            
		             </div> <!-- End .module-body -->
		
		        </div>  <!-- End .module -->
				
		    </div>
		    
		    <div class="grid_12">
		    <fieldset>
		    	&nbsp;
		    	<input type="hidden" id="sponcer_id" name="sponcer_id" value="<?php if(isset($_POST['sponcer_id'])){ echo $_POST['sponcer_id']; } ?>">
		        <input class="submit-green" type="submit" name="save" value="Submit" /> 
		        <input class="submit-gray" type="button" value="Back" onclick="window.location='sponcers.php'" />
		    </fieldset>
		    </div>
		    <div style="clear:both;"></div>
		 </form>    
        </div> <!-- End .container_12 -->
		
<?php include_once("footer.php"); ?>           
<?php
if(isset($msg_success) && $msg_success!="")
{
	$notymessage=$msg_success;
	$msgtype="success";
}
else if(isset($msg_error) && $msg_error!="")
{
	$msgtype="error";
	$notymessage=$msg_error;
}
else
{
	$msgtype="";
}
if($msgtype!="")
{
?>        
<script language="javascript">
	$(document).ready(function(){
		
		var options = $.parseJSON('{"text":"<?php echo $notymessage; ?>","layout":"top","type":"<?php echo $msgtype; ?>"}');
		noty(options);
		
	});
</script>  
<?php
}
?>
<script>

function validateaddeditsponcer()
{
	
	if(trim(document.getElementById("name").value)=="")
	{
		alertify.alert("Sponcer Name field is required!");
		document.getElementById("name").focus();
		return false;
	}
	else if(trim(document.getElementById("link").value)=="")
	{
		alertify.alert("Sponcer link field is required!");
		document.getElementById("link").focus();
		return false;
	}
    else if(trim(document.getElementById("sponcer_photo").value)=="" && trim(document.getElementById('image_url').value)=="")
	{
		alertify.alert("Sponcer image field is required!");
		document.getElementById("sponcer_photo").focus();
		return false;
	}
    
}
</script>       